package com.waimai.controllor.meituan;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;

import com.waimai.adapter.RestaurantAdapter;
import com.waimai.constant.URLConstant;
import com.waimai.controllor.BaseControllor;
import com.waimai.controllor.HttpControllor;
import com.waimai.controllor.Interface.IHtmlControllor;
import com.waimai.controllor.Interface.IHttpControllor;
import com.waimai.entity.Address;
import com.waimai.entity.Restaurant;
import com.waimai.entity.Sale;
import com.waimai.log.Log;
import com.waimai.utils.HtmlParserUtils;
import com.waimai.utils.htmlStrUtils;

public class MeituanHttpControllor extends BaseControllor implements
		IHttpControllor {

	private static MeituanHttpControllor controllor;

	public static MeituanHttpControllor getInstance() {
		if (controllor == null) {
			controllor = new MeituanHttpControllor();
		}
		return controllor;
	}

	@Override
	public Map<String, RestaurantAdapter> getRestaurants(Address... addresses) {
		URI uri = null;
		Map<String, RestaurantAdapter> restaurantMap = new HashMap<String, RestaurantAdapter>();
		for (Address address : addresses) {
			try {
				uri = new URI(URLConstant.URL_MEITUAN
						+ address.getMeituanHref());
			} catch (URISyntaxException e) {
				Log.e(e, "meituan uri error");
				e.printStackTrace();
			}
			HttpControllor controllor = HttpControllor.getInstance();
			restaurantMap.putAll(getRestaurantsByBytes(
					controllor.getHtmlBytes(uri), address));
		}
		return restaurantMap;
	}

	/**
	 * 
	 * 
	 * @param bytes
	 * @return
	 * 
	 */
	private Map<String, RestaurantAdapter> getRestaurantsByBytes(byte[] bytes,
			Address address) {
		IHtmlControllor htmlControllor = MeituanHtmlControllor.getInstance();
		before();
		Map<String, RestaurantAdapter> restaurantMap = new HashMap<String, RestaurantAdapter>();
		if (bytes != null) {

			// 按键值对查
			// 减钱商家
			NodeList nodeList = HtmlParserUtils.getNodeListByBytes(bytes,
					URLConstant.CHARSET, new HasAttributeFilter("data-icon",
							"i-minus"));
			Log.i("meituanRestaurantsNum = " + nodeList.size());
			if (nodeList.size() != 0) {
				for (int i = 0; i < nodeList.size(); i++) {
					Node minusNode = nodeList.elementAt(i);
					ArrayList<Sale> saleList = htmlStrUtils
							.getMeituanSaleList(minusNode.toPlainTextString());
					// 获得最上层节点
					// 不费时间，三个父节点
					Node parentNode = minusNode;
					while (true) {
						parentNode = parentNode.getParent();
						String text = parentNode.getText();
						if (text.contains("data-title")) {
							break;
						}
					}

					// href
					String hrefUrl = htmlControllor.getHrefUrl(parentNode);
					if ("".equals(hrefUrl)) {
						continue;
					}

					// 图标
					String imgUrl = htmlControllor.getImgUrl(parentNode);

					Node contentNode = HtmlParserUtils.getChildNode(parentNode,
							new HasAttributeFilter("class", "content"));
					String name = "";
					String startPrice = "";
					String sendPrice = "";
					String totalPerMon = "";
					if (contentNode != null) {
						name = htmlControllor.getName(contentNode);
						startPrice = htmlControllor.getStartPrice(contentNode);
						sendPrice = htmlControllor.getSendPrice(contentNode);
						totalPerMon = htmlControllor
								.getTotalPerMon(contentNode);
					} else {
						continue;
					}

					// 休息
					int status = htmlControllor.getStatus(parentNode);
					// 饮料
					// data-icon="i-ph17x17"
					ArrayList<String> drink = htmlControllor
							.getDrink(parentNode);

					// 送餐时间
					String sendTime = htmlControllor.getSendTime(parentNode);

					// 公司
					int company = htmlControllor.getCompany();

					int maxBack = htmlControllor.getMaxBack(null);

					String id = company + hrefUrl;

					RestaurantAdapter r = new RestaurantAdapter(new Restaurant(
							id, name, saleList, hrefUrl, drink, company,
							maxBack, status, imgUrl, totalPerMon), address,
							sendTime, startPrice, sendPrice);
					restaurantMap.put(r.getId(), r);
				}
			}
		} else {
			Log.e("bytes is null");
		}
		after("getMeituanRestaurants");
		return restaurantMap;
	}

}
